package edu.bit.water.controller;

import edu.bit.water.entity.*;
import edu.bit.water.service.TableWaterSavingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/table_water_saving")
public class TableWaterSavingController {

    @Autowired
    private TableWaterSavingService tableWaterSavingService;

    /**
     * 查询META_MANAGEMENT_INDICATOR中的记录
     * @return META_MANAGEMENT_INDICATOR中的所有记录
     */
    @RequestMapping("/management_select")
    @ResponseBody
    public Map<String, Object> managementSelect(int last_id){
        Map<String, Object> modelMap = new HashMap<>();
        List<ManagementIndicator> list = tableWaterSavingService.managementSelect(last_id);
        //记录list中所有不同的assessmentIndicator
        List<String> assessmentIndicator = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            boolean flag = true;
            for (int j = 0; j < assessmentIndicator.size(); j++) {
                if (list.get(i).getAssessmentIndicator().equals(assessmentIndicator.get(j))) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                assessmentIndicator.add(list.get(i).getAssessmentIndicator());
            }
        }
        //重新排序list
        List<ManagementIndicator> sortList = new ArrayList<ManagementIndicator>();
        for (int i = 0; i < assessmentIndicator.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getAssessmentIndicator().equals(assessmentIndicator.get(i))) {
                    sortList.add(list.get(j));
                }
            }
        }

        modelMap.put("status", "success");
        modelMap.put("rows", sortList);
        return modelMap;
    }

    /**
     * 查询META_TECHNICAL_INDICATOR中的记录
     * @return META_TECHNICAL_INDICATOR中的所有记录
     */
    @RequestMapping("/technical_select")
    @ResponseBody
    public Map<String, Object> TechnicalSelect(int last_id){
        Map<String, Object> modelMap = new HashMap<>();
        List<TechnicalIndicator> list = tableWaterSavingService.technicalSelect(last_id);
        //记录list中所有不同的assessmentContent
        List<String> assessmentContent = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            boolean flag = true;
            for (int j = 0; j < assessmentContent.size(); j++) {
                if (list.get(i).getAssessmentContent().equals(assessmentContent.get(j))) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                assessmentContent.add(list.get(i).getAssessmentContent());
            }
        }
        //重新排序list
        List<TechnicalIndicator> sortList = new ArrayList<TechnicalIndicator>();
        for (int i = 0; i < assessmentContent.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getAssessmentContent().equals(assessmentContent.get(i))) {
                    sortList.add(list.get(j));
                }
            }
        }
        modelMap.put("status", "success");
        modelMap.put("rows", sortList);
        return modelMap;
    }

    /**
     * 更新记录
     * @param managementIndicator 标签记录实体
     * @return 成功返回1,失败返回0
     */
    @RequestMapping("/management_update")
    @ResponseBody
    public Map<String, String> managementUpdate(ManagementIndicator managementIndicator){
        return tableWaterSavingService.managementUpdate(managementIndicator);
    }

    /**
     * 更新记录
     * @param technicalIndicator 标签记录实体
     * @return 成功返回1,失败返回0
     */
    @RequestMapping("/technical_update")
    @ResponseBody
    public Map<String, String> technicalUpdate(TechnicalIndicator technicalIndicator){
        return tableWaterSavingService.technicalUpdate(technicalIndicator);
    }

    /**
     * 更新记录
     * @param labelWaterSaving 标签记录实体
     * @return 成功返回1,失败返回0
     */
    @RequestMapping("/label_update")
    @ResponseBody
    public Map<String, String> labelUpdate(LabelWaterSaving labelWaterSaving){
        return tableWaterSavingService.labelUpdate(labelWaterSaving);
    }

    /**
     * 获取管理考核指标注释
     * @param  standardNumber
     * @return 成功返回1,失败返回0
     */
    @RequestMapping("/getManagementNote")
    @ResponseBody
    public Map<String, Object> getManagementNote(String standardNumber){
        Map<String, Object> modelMap = new HashMap<>();
        List<ManagementNote> list = tableWaterSavingService.getManagementNote(standardNumber);
        modelMap.put("status", "success");
        modelMap.put("rows", list);
        return modelMap;
    }

    /**
     * 获取技术考核指标注释
     * @param  standardNumber
     * @return 成功返回1,失败返回0
     */
    @RequestMapping("/getTechnicalNote")
    @ResponseBody
    public Map<String, Object> getTechnicalNote(String standardNumber){
        Map<String, Object> modelMap = new HashMap<>();
        List<TechnicalNote> list = tableWaterSavingService.getTechnicalNote(standardNumber);
        modelMap.put("status", "success");
        modelMap.put("rows", list);
        return modelMap;
    }
}